//
//  XTStatusCell.swift
//  MyBlog
//
//  Created by victor on 17/3/23.
//  Copyright © 2017年 victor. All rights reserved.
//

import UIKit

class XTStatusCell: UITableViewCell {
    
    //MARK:设置属性
    var viewModel : XTStatusViewModel? {
        didSet {
            // 微博文本
            statusLabel.text = viewModel?.status.text
            // 姓名
            nameLabel.text = viewModel?.status.user?.screen_name
            // 设置会员图标 - 直接获取属性，不需要计算
            memberIconView.image = viewModel?.memberIcon
            // 认证图标
            vipIconView.image = viewModel?.vipIcon
            // 用户图像
            iconView.xt_setImage(urlString: viewModel?.status.user?.profile_image_url,
                                 placeholderImage: UIImage(named: "avatar_default_big"),
                                 isAvatar: true)
            
            // 底部工具栏
            toolBar.viewModel = viewModel
            
            // 测试修改配图视图的高度
            picView.viewModel = viewModel
            
            // 设置配图视图的 url 数据
            // 测试4张用户图像
//            if (viewModel?.status.pic_urls?.count)! > 4 {
//                // 修改数组 -> 将末尾的数据全部删除
//                var picURLs = viewModel!.status.pic_urls
//                picURLs?.removeSubrange(((picURLs?.startIndex)! + 4)..<(picURLs?.endIndex)!)
//                picView.urls = picURLs
//            } else {
//                picView.urls = viewModel?.status.pic_urls
//            }
        }
    }
    
    /// 颜色分割线
    fileprivate lazy var separatorView : UIView = UIView()
    /// 头像
    fileprivate lazy var iconView = UIImageView()
    /// 姓名
    fileprivate lazy var nameLabel = UILabel()
    /// 会员图标
    fileprivate lazy var memberIconView = UIImageView()
    /// 时间
    fileprivate lazy var timeLabel = UILabel()
    /// 来源
    fileprivate lazy var sourceLabel = UILabel()
    /// 认证图标
    fileprivate lazy var vipIconView = UIImageView()
    /// 微博正文
    fileprivate lazy var statusLabel = UILabel()
    /// 底部工具栏
    fileprivate lazy var toolBar : XTStatusToolBar = XTStatusToolBar()
    /// 配图视图
    fileprivate lazy var picView : XTStatusPictureView = XTStatusPictureView()
    
    //MARK:设置UI
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        
        selectionStyle = .none
        setupUI()
        
        // 离屏渲染 - 异步绘制 一旦打开就是离屏渲染了
        self.layer.drawsAsynchronously = true
        
        // 栅格化 - 异步绘制之后 会生成一张独立的图像，cell在屏幕上滚动的时候，本质上滚动的是这张图片
        // cell 优化，要尽量减少图层的数量，相当于就只有一层
        // 停止滚动之后，可以接收监听
        self.layer.shouldRasterize = true
        
        // 使用 '栅格化' 必须注意指定分辨率
        self.layer.rasterizationScale = UIScreen.main.scale
    }
    
    fileprivate func setupUI() {
        separatorView.backgroundColor = UIColor.cz_color(withHex: 0xf2f2f2)
        
        nameLabel.textColor = UIColor.orange
        nameLabel.font = UIFont.systemFont(ofSize: 15)
        
        timeLabel.text = "11:20"
        timeLabel.textColor = UIColor.orange
        timeLabel.font = UIFont.systemFont(ofSize: 10)
        
        sourceLabel.text = "来源"
        sourceLabel.textColor = UIColor.darkGray
        sourceLabel.font = UIFont.systemFont(ofSize: 10)
        
        statusLabel.textColor = UIColor.black
        statusLabel.numberOfLines = 0
        statusLabel.font = UIFont.systemFont(ofSize: 15)
        
        toolBar.backgroundColor = UIColor.lightGray
        
        addSubview(separatorView)
        addSubview(iconView)
        addSubview(nameLabel)
        addSubview(memberIconView)
        addSubview(timeLabel)
        addSubview(sourceLabel)
        addSubview(statusLabel)
        addSubview(vipIconView)
        addSubview(picView)
        addSubview(toolBar)
        
        separatorView.snp.makeConstraints { (make) in
            make.top.equalTo(self.snp.top)
            make.left.equalTo(self.snp.left)
            make.right.equalTo(self.snp.right)
            make.height.equalTo(12)
        }
        
        iconView.snp.makeConstraints { (make) in
            make.top.equalTo(24)
            make.left.equalTo(12)
            make.width.equalTo(34)
            make.height.equalTo(34)
        }
        
        nameLabel.snp.makeConstraints { (make) in
            make.top.equalTo(iconView.snp.top).offset(3)
            make.left.equalTo(iconView.snp.right).offset(12)
        }
        
        memberIconView.snp.makeConstraints { (make) in
            make.left.equalTo(nameLabel.snp.right).offset(4)
            make.centerY.equalTo(nameLabel.snp.centerY)
        }
        
        timeLabel.snp.makeConstraints { (make) in
            make.left.equalTo(nameLabel.snp.left)
            make.bottom.equalTo(iconView.snp.bottom)
        }
        
        sourceLabel.snp.makeConstraints { (make) in
            make.left.equalTo(timeLabel.snp.right).offset(8)
            make.centerY.equalTo(timeLabel.snp.centerY)
        }
        
        statusLabel.snp.makeConstraints { (make) in
            make.top.equalTo(iconView.snp.bottom).offset(11)
            make.left.equalTo(iconView.snp.left)
            make.right.equalTo(self.snp.right).offset(-10)
            make.bottom.equalTo(picView.snp.top)
        }
        
        vipIconView.snp.makeConstraints { (make) in
            make.centerX.equalTo(iconView.snp.right).offset(-4)
            make.centerY.equalTo(iconView.snp.bottom).offset(-4)
        }
        
        picView.snp.makeConstraints { (make) in
            make.top.equalTo(statusLabel.snp.bottom)
            make.left.equalTo(statusLabel.snp.left)
            make.right.equalTo(self.snp.right).offset(-12)
            make.bottom.equalTo(toolBar.snp.top).offset(-12)
            make.height.equalTo(385)
        }
        
        toolBar.snp.makeConstraints { (make) in
            make.left.equalTo(self.snp.left)
            make.bottom.equalTo(self.snp.bottom)
            make.right.equalTo(self.snp.right)
            make.height.equalTo(35)
        }
        
        nameLabel.sizeToFit()
        statusLabel.sizeToFit()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

}
